export function is(val: unknown, type: string) {
    return Object.prototype.toString.call(val) === `[object ${type}]`
}

// 是否为字符串
export function isString(val: unknown): val is string {
    return is(val, 'String')
}

// 是否为数字
export function isNumber(val: unknown): val is number {
    return is(val, 'Number')
}

// 是否为布尔
export function isBoolean(val: unknown): val is boolean {
    return is(val, 'Boolean')
}

// 是否为方法
export function isFunction(val: unknown): val is Function {
    return typeof val === 'function'
}

// 是否为对象
export function isObject(val: any): val is Record<any, any> {
    return val !== null && is(val, 'Object')
}

// 是否为数组
export function isArray(val: any): val is Array<any> {
    return val && Array.isArray(val)
}

// 是否为空
export function isEmpty<T = unknown> (val: T): val is T {
    if(isArray(val) || isString(val)) {
        return val.length === 0
    }
    if(val instanceof Map || val instanceof Set) {
        return val.size === 0
    }
    if(isObject(val)) {
        return Object.keys(val).length === 0
    }

    return false
}


const fn = {
    string: isString,
    number: isNumber,
    boolean: isBoolean,
    array: isArray,
    object: isObject,
}

export function getTypeof(val: any): string {
    let res = ''
    for(let key in fn) {
        if((fn as any)[key](val)) {
            res = key
        }
    }

    return res
}